<?php
/**
 * (exporter)
 */
namespace App\Exporter\Shipping;

use App\Models\Inventory\PublicCn;
use App\Models\Product\ProductClassify;
use App\Models\Shipping\SSku;
use App\Models\Shipping\SSkuApply;
use App\Util\User;
use Encore\Admin\Grid;
use Encore\Admin\Grid\Exporters\ExcelExporter;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
class SkuExporter extends ExcelExporter implements WithMapping,WithHeadings
{
    protected $shippingDate;

    public function __construct($shippingDate,Grid $grid = NULL)
    {
        parent::__construct($grid);

        $this->shippingDate = $shippingDate;
    }

    protected $fileName = '提货产品.csv';

    public function headings(): array
    {
        $head1 = ['品类','站点','SKU','季节性','产品名称',
            '3天AVG','7天AVG','15天AVG','30天AVG','AVG',
            'FBA在库','FBA在途', '公共在库','国内在库','国内在途', '海外仓在库','海外仓在途',
            '生产周期','空运时效','海运时效','空运提货库存安全系数','海运提货库存安全系数','提货库存目前系数'];
        $head2 = [];
        foreach ($this->shippingDate as $label){
            $head2[] = $label.'需求';
        }
//        $head3 = ['差值','提货系数','状态','提货员'];
        $head3 = ['差值','状态','提货员'];
        return array_merge($head1,$head2,$head3);
    }

    public function map($model) : array
    {
        $publicCn = PublicCn::getInstant($model->psku_id);
        $data1 = [
            $model->skuClassify->title ?? '',
            $model->area->area ?? '',
            $model->sku->psku_code ?? '',
            ProductClassify::$_season[$model->skuClassify->season ?? 3],
            $model->sku->psku_cn ?? '',
            $model->_avg->avg3 ?? 0,
            $model->_avg->avg7 ?? 0,
            $model->_avg->avg15 ?? 0,
            $model->_avg->avg30 ?? 0,
            $model->_avg->avg ?? 0,
            $model->stock->fba_instant_num ?? 0,
            $model->stock->fba_transit_num ?? 0,
            $publicCn,
            $model->stock->cn_instant_num ?? 0,
            $model->stock->cn_transit_num ?? 0,
            $model->stock->os_instant_num ?? 0,
            $model->stock->os_transit_num ?? 0,
            $model->purchase_cycle ?: 0,
            $model->air_day ?: 0,
            $model->sea_day ?: 0,
            $model->air_factor,
            $model->sea_factor,
            SSku::getShippingCurrentFactor($model,$publicCn)];
        $data2 = [];
        $applyNum = SSkuApply::getApplyNum($model->area_id,$model->psku_id,array_key_first($this->shippingDate),array_key_last($this->shippingDate));
        $applyNumTotal = 0;
        foreach ($this->shippingDate as $date => $label){
            $apply = array_key_exists($date,$applyNum) ? $applyNum[$date] : 0;
            $data2[] = $apply;
            $applyNumTotal += $apply;
        }
        $data3 = [
            ($model->stock->cn_transit_num ?? 0) - $applyNumTotal,
//            SSku::getShippingFactor($model,$publicCn,$applyNumTotal),
            SSku::$_status[$model->status],
            $model->seller_id ? User::getUsernameById($model->seller_id) : '',
        ];
        return array_merge($data1,$data2,$data3);
    }
}
